package dao.impl;

import entity.ClassEntity;
import dao.ClassDao;
import dto.GetSameGradeClassDTO;
import util.JDBCUtil;

import java.util.List;
import java.util.Map;

public class ClassDaoImpl extends JDBCUtil implements ClassDao {
    @Override
    public List<Map<String, Object>> getClassInfo(String name) {
        String sql;
        List<Map<String, Object>> mapList;
        if ("null".equalsIgnoreCase(name)) {
            sql = "select * from class_info";
            mapList = queryForList(sql);
        } else if (name != null && !name.trim().isEmpty()) {
            sql = "select * from class_info where class_name like ?";
            String className = "%" + name + "%";
            mapList = queryForList(sql, className);
        } else {
            sql = "select * from class_info";
            mapList = queryForList(sql);
        }
        return mapList;
    }

    @Override
    public int addClassInfo(ClassEntity classEntity) {
        String sql = "insert into class_info (id,specialty_id,teacher_id,class_name,class_code,grade) values (?,?,?,?,?,?)";
        return insert(sql, classEntity.getId(), classEntity.getSpecialtyId(), classEntity.getTeacherId(), classEntity.getClassName(), classEntity.getClassCode(), classEntity.getGrade());
    }

    @Override
    public int updateClassInfo(ClassEntity classEntity) {
        String sql = "update class_info set specialty_id = ?,teacher_id = ?,class_name = ?,class_code = ? where id = ?";
        return update(sql, classEntity.getSpecialtyId(), classEntity.getTeacherId(), classEntity.getClassName(), classEntity.getClassCode(), classEntity.getId());
    }

    @Override
    public int deleteClass(String classId) {
        String sql = "delete class_info from class_info where id = ?";
        return delete(sql, classId);
    }

    @Override
    public int batchDeleteClass(List<String> ids) {
        if (ids == null || ids.isEmpty()) {
            throw new IllegalArgumentException("ID列表不能为空");
        }

        StringBuilder placeholders = new StringBuilder();
        for (int i = 0; i < ids.size(); i++) {
            placeholders.append("?");
            if (i < ids.size() - 1) {
                placeholders.append(",");
            }
        }

        String sql = "delete from class_info where id in (" + placeholders + ")";
        return delete(sql, ids.toArray());
    }

    @Override
    public List<Map<String, Object>> getClassInfoBySpecialtyId(String specialtyId, String grade) {
        String sql = "select * from class_info where specialty_id = ? and grade = ?";
        return queryForList(sql, specialtyId, grade);
    }

    @Override
    public List<Map<String, Object>> getClassStudentInfo(String classId) {
        String sql = "select * from student_info where class_id = ?";
        return queryForList(sql, classId);
    }

    @Override
    public Map<String, Object> getClassInfoById(String classId) {
        String sql = "select * from class_info where id = ?";
        List<Map<String, Object>> mapList = queryForList(sql, classId);
        if (mapList.isEmpty()) {
            return null;
        }
        return mapList.get(0);
    }

    @Override
    public int removeClassStudent(String studentId) {
        String sql = "update student_info set class_id = NULL where id = ?";
        return update(sql, studentId);
    }

    @Override
    public List<Map<String, Object>> getSameGradeClass(GetSameGradeClassDTO dto) {
        String sql = "select class_name,id from class_info where grade = ? and specialty_id = ?";
        return queryForList(sql, dto.getGrade(), dto.getSpecialtyId());
    }
}
